import datetime

from PySide6.QtCore import Qt
from PySide6.QtGui import QFont
from PySide6.QtWidgets import QWidget, QVBoxLayout, QStyle, QPushButton, QLabel, QHBoxLayout, QLineEdit

import RegisterWindow
import DBAccessor
from AccountManager import AccountManager


class Login_Dialog(QWidget):
    Account_Manager: AccountManager = None
    RES_X: int = None
    RES_Y: int = None
    DBAccessor = None
    TitlePointSize: int = None
    AttributePointSize: int = None
    LineEditPointSize: int = None
    Register_Window: RegisterWindow.Register_Dialog = None

    def __init__(self, account_manager: AccountManager, res_x: int, res_y: int, dbaccessor):  # Surface 684*456
        super().__init__()
        self.Account_Manager = account_manager
        self.RES_X = res_x
        self.RES_Y = res_y
        self.DBAccessor = dbaccessor

        # 初始化参数
        self.TitlePointSize = int(res_x / 75)
        self.AttributePointSize = int(res_x / 100)

        # 设置窗口属性
        self.setWindowIcon(self.style().standardPixmap(QStyle.StandardPixmap.SP_TrashIcon))  # 图标
        self.setWindowTitle("Magic Calendar: Login Dialog (*^▽^*)")  # 标题
        self.setFixedSize(int(res_x / 4), int(res_y / 4))  # 最大窗口大小

        # 标题部分
        temp_font = QFont("微软雅黑", self.TitlePointSize)
        temp_font.setBold(True)
        self.label_title = QLabel("欢迎使用个人日程管理系统\n\\(*^▽^*)/")
        self.label_title.setFont(temp_font)
        self.label_title.setAlignment(Qt.AlignmentFlag.AlignCenter)
        self.label_title.setMinimumHeight(self.TitlePointSize * 5)

        # 账号输入
        self.layout_h_account = QHBoxLayout()  # 输入布局
        temp_font.setPointSize(self.AttributePointSize)
        temp_font.setBold(False)
        # Label
        self.label_account = QLabel("账号 ")
        self.label_account.setFont(temp_font)
        self.label_account.setAlignment(Qt.AlignmentFlag.AlignCenter)
        self.label_account.setMaximumHeight(self.AttributePointSize * 2)
        self.layout_h_account.addWidget(self.label_account)
        # LineEdit
        self.lineedit_account = QLineEdit()
        self.lineedit_account.setPlaceholderText("这里输入Uid~")
        self.lineedit_account.setFont(temp_font)
        self.layout_h_account.addWidget(self.lineedit_account)

        # 密码输入
        self.layout_h_password = QHBoxLayout()  # 输入布局
        # Label
        self.label_password = QLabel("密码 ")
        self.label_password.setFont(temp_font)
        self.label_password.setAlignment(Qt.AlignmentFlag.AlignCenter)
        self.label_password.setMaximumHeight(self.AttributePointSize * 2)
        self.layout_h_password.addWidget(self.label_password)
        # LineEdit
        self.lineedit_password = QLineEdit()
        self.lineedit_password.setPlaceholderText("这里输入密码~")
        self.lineedit_password.setFont(temp_font)
        self.lineedit_password.setEchoMode(QLineEdit.EchoMode.Password)
        self.layout_h_password.addWidget(self.lineedit_password)

        # 按钮
        temp_font.setPointSize(int(self.AttributePointSize * 5 / 6))
        self.layout_h_button = QHBoxLayout()
        self.button_confirm = QPushButton()
        self.button_confirm.setText("登录")
        self.button_confirm.setFont(temp_font)
        self.button_confirm.clicked.connect(self.click_confirm)  # 绑定调用方法
        self.layout_h_button.addWidget(self.button_confirm)
        self.button_register = QPushButton()
        self.button_register.setText("注册")
        self.button_register.setFont(temp_font)
        self.button_register.clicked.connect(self.click_register)  # 绑定调用方法
        self.layout_h_button.addWidget(self.button_register)

        # 总布局
        layout_main = QVBoxLayout(self)
        layout_main.addWidget(self.label_title)
        layout_main.addLayout(self.layout_h_account)
        layout_main.addLayout(self.layout_h_password)
        layout_main.addLayout(self.layout_h_button)

    def closeEvent(self, event):
        """
        重写关闭事件
        使主窗口退出时结束程序
        """
        if self.Register_Window is not None:
            self.Register_Window.close()
        super().closeEvent(event)

    def click_confirm(self):
        # 处理登录操作
        uid = self.lineedit_account.text()
        password = self.lineedit_password.text()
        if uid == "":
            self.label_title.setText("账号不能为空！！！")
        elif password == "":
            self.label_title.setText("密码不能为空！！！")
        else:
            password_md5 = DBAccessor.encode_password(password)  # 加密密码
            result: tuple = self.DBAccessor.select_user(uid=uid, password_md5=password_md5)
            if result == ():
                self.label_title.setText("登录失败！\n账号不存在或密码错误！")
            else:
                # 登录成功
                self.label_title.setText("登录成功！")
                self.lineedit_account.setReadOnly(True)
                self.lineedit_password.setReadOnly(True)
                self.Account_Manager.uid = result[0]
                self.Account_Manager.nickname = result[1]
                # self.Account_Manager.privilege_level = result[2]
                # self.Account_Manager.default_abs = result[3]
                # self.Account_Manager.default_ext = result[4]
                self.close()

    def click_register(self):
        # 启动注册窗口
        self.Register_Window = RegisterWindow.Register_Dialog(self, self.RES_X, self.RES_Y, self.DBAccessor)
        self.Register_Window.show()

